package com.cardosystems.infra.pages;

import java.util.List;

import net.sf.cglib.core.Local;

import org.apache.tools.ant.util.KeepAliveInputStream;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

import com.cardosystems.infra.enums.Gender;
import com.cardosystems.infra.enums.ProductTypes;
import com.gargoylesoftware.htmlunit.javascript.host.Location;

public class ProductRegistrationPage extends AbstractPage {

	// @FindBy(id="product_type")
	@FindBy(xpath = "//*[@id=\"product_type\"]")
	WebElement productType;

	@FindBy(id = "purchase_date")
	WebElement purchaseDate;

	@FindBy(id = "purchase_store")
	WebElement purchaseStore;

	@FindBy(id = "1_1_5")
	WebElement birthYear;

	@FindBy(id = "location")
	WebElement loaction;

	@FindBy(id = "new_products")
	WebElement newProducts;

	@FindBy(id = "funclub")
	WebElement funClub;

	@FindBy(id = "submit")
	WebElement submitBtn;

	public ProductRegistrationPage(WebDriver driver) {
		super(driver);
		// driver.switchTo().frame("myframe");
		PageFactory.initElements(driver, this);
	}

	public CardoCommunityHomePage setProductProperties(Gender gender, String year,
			String loactionName, ProductTypes type, String day,
			String storeName, boolean newCardoProduct,
			boolean cardoCommunityFanClub) throws Exception {
		driver.findElement(By.id(gender.getGender())).click();
		selectFromList(year, birthYear);
		loaction.sendKeys(loactionName);
		Thread.sleep(500);
		loaction.sendKeys(Keys.DOWN);
		Thread.sleep(500);
		selectFromList(type.getType(), productType);
		purchaseDate.click();
		selectDate(day);
		purchaseStore.sendKeys(storeName);

		if (!newCardoProduct) {
			newProducts.click();
		}
		if (!cardoCommunityFanClub) {
			funClub.click();
		}
		submitBtn.click();
		return new CardoCommunityHomePage(driver);
	}

	@Override
	public boolean isInModule() {
		// TODO Auto-generated method stub
		return false;
	}

	private void selectDate(String day) throws Exception {
		WebElement dateWidget = driver.findElement(By
				.xpath("//*[@id=\"global_page_user-signup-index\"]/div[2]"));
		List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));
		List<WebElement> columns = dateWidget.findElements(By.tagName("td"));

		for (WebElement cell : columns) {
			// Select 13th Date
			if (cell.getText().equals("13")) {
				cell.click();
				// cell.findElement(By.linkText("13")).click();
				break;
			}
		}
	}

	private void selectFromList(String valueToSelect, WebElement select)
			throws Exception {
		Select dropDown = new Select(select);
		String selected = dropDown.getFirstSelectedOption().getText();
		List<WebElement> Options = dropDown.getOptions();
		for (WebElement option : Options) {
			if (option.getText().equals(valueToSelect)) {
				option.click(); // select option here;
				return;
			}
		}
	}

}
